<?php

namespace App\Repositories\Auth;

use App\Models\Auth\Permission;
use App\Models\Auth\Role;
use App\Repositories\Repository;
use Illuminate\Support\Facades\DB;


/**
 *
 */
class RoleRepository extends Repository
{

    /**
     *
     * 角色列表
     * @author Paco
     * @createTime 2018/8/21 10:02
     * @return mixed
     */
    public function list()
    {
        $roles = Role::query()->with('permissions')
            ->paginate($this->page_size, ['*'], 'page', $this->page);
        return $this->respondWith($roles);
    }

    /**
     *
     * 角色详情
     * @author Paco
     * @createTime 2018/8/21 10:01
     * @param $role_id
     * @return mixed
     */
    public function show($role_id)
    {
        $role = Role::query()->with('permissions')->find($role_id);

        return $this->respondWith( $role);
    }

    /**
     * 添加角色
     *
     * @author Pako
     * @createTime 2018/8/19 17:02
     * @param $data
     * @return mixed
     */
    public function store($data)
    {
        $permission_data = [];

        DB::beginTransaction();
        try {
            $role = Role::query()->create($data);
            foreach ($data['permissions'] as $permission) {
                $permission_data[] = ['permission_id' => $permission, 'role_id' => $role->role_id];
            }
            if (count($permission_data)) {
                DB::table('auth_role_permission')->insert($permission_data);
            }
            $this->log_done('添加角色', $data);
            DB::commit();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->respondError(10110);
        }
    }

    /**
     * 更新角色与权限
     *
     * @author Pako
     * @createTime 2018/8/19 17:00
     * @param $data
     * @return mixed
     */
    public function save($data)
    {
        $role = Role::query()->where('role_id', $data['role_id'])->first();
        if (!$role) {
            return $this->respondError(10109);
        }
        //组装权限数组
        $permission_data = [];
        try {
            $role->fill($data)->save();
            DB::table('auth_role_permission')->where('role_id', $role->role_id)->delete();
            foreach ($data['permissions'] as $permission) {
                $permission_data[] = ['permission_id' => $permission, 'role_id' => $role->role_id];
            }
            DB::table('auth_role_permission')->insert($permission_data);
            $this->log_done('更新角色', $data);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }

    public function saveStatus($data)
    {
        $role = Role::query()->where('role_id', $data['role_id'])->first();
        if (!$role) {
            return $this->respondError(10109);
        }
        try {
            $role->fill($data)->save();
            $this->log_done('更新角色状态', $data);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $e;
            return $this->respondError(10108);
        }
    }

    /**
     *
     * 删除角色
     * @author Paco
     * @createTime 2018/8/21 10:01
     * @param $role_id
     * @return mixed
     */
    public function del($role_id)
    {
        try {
            $role = Role::query()->where('role_id', $role_id)->first();
            $role->permissions()->detach();
            Role::query()->where("role_id", "=", $role_id)->delete();
            $this->log_done('删除角色', $role);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10107);
        }
    }

    /**
     *
     * 获取角色权限
     * @author Paco
     * @createTime 2018/8/21 10:01
     * @return array
     */
    private function getPermission()
    {
        return Permission::query()->get();
    }


}

